<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

use App\Http\Requests; 
use App\Http\Controllers\Controller;
use App\Http\Model\Link;


class LinkController extends Controller
{
   public function changeOrder()
    {   
       
       $input = Input::except('_token');
       // dd($input);


       $input['id'];
       $input['order'];
//       通过id找到要修改记录
       $link = Link::find($input['id']);
       $link->order = $input['order'];
       $res = $link->save();


       $data = [];
       if($res){
            $data['status'] = 0;
            $data['msg']  = '修改成功';
       }else{
           $data['status'] = 1;
           $data['msg']  = '修改失败';
       }
       return $data;
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) 
    {
        // 加载友情链接模板
        $count = $request -> input('count',4);
        $request = $request -> all();
        // 查询数据并分页
        $data = Link::paginate($count);
        // 加载模版
        return view('admin.link.index',['data'=>$data,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 加载添加友情链接页面
        return view('admin.link.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
            //获取到用户提交过来的数据
        $data = $request ->except('_token');
        // dd($data);
        
        // 处理插入
        $link = new Link;
        $link->order = $data['order'];
        $link->title = $data['title'];
        $link->url = $data['url'];
        // 判断 跳转页面

        if($link->save()){
            return redirect('/admin/link');
        }else{
            return back()->with('errors','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response*/
     
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       $data = [];
       $res =  Link::find($id)->delete();
        //  删除成功
       if($res){
            $data['status']= 0;
            $data['msg']='删除成功';
       }else{
           $data['status']= 1;
           $data['msg']='删除失败';
       }
       return $data;
    }
}
